<template>
  <div>
    <div class="content">
      <div v-for="(item, i) in data" :key="i" class="contentBox">
        <el-card shadow="always" :body-style="{ padding: '0px' }">
          <el-image :src="item.imgurl" fit="cover" @click="playMv(item)" lazy>
            <div slot="placeholder" class="image-slot">
              <el-image
                src="https://s4.music.126.net/style/web2/img/default/default_album.jpg"
                @click="playMv(item)"
                lazy
              ></el-image>
            </div>
          </el-image>
          <div style="padding: 14px">
            <el-tooltip
              class="item"
              effect="dark"
              :content="item.name"
              placement="top"
            >
              <div class="desc">
                {{ item.name }}
              </div>
            </el-tooltip>
          </div>
        </el-card>
      </div>
    </div>
    <div class="loading" v-if="loading === true">
      <i class="el-icon-loading"></i>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';

export default {
  mounted() {
    this.id = window.sessionStorage.getItem('artists');
    this.getData();
  },
  // computed: {
  //   ...mapState(["resArtists"]),
  // },
  // watch: {
  //   resArtists: function (newVal, oldVal) {
  //     this.id = newVal.resArtists;
  //     this.getData();
  //   },
  // },
  data() {
    return {
      id: null,
      data: null,
      loading: false,
    };
  },
  methods: {
    ...mapActions(['saveMVurl', 'saveResMv', 'saveMvBes']),
    async getData() {
      this.data = null;
      this.$api.artistMv({ id: parseInt(this.id), cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.data = ret.mvs;
      });
    },

    // 点击播放MV
    async playMv(e) {
      // 获取到MV的地址
      this.$api.mvUrl({ id: parseInt(e.id), cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveMVurl({ mvUrl: ret.data.url });
        this.getResMv(e.id);
        this.getItem(e.id);
      });
    },

    // 获取相似MV
    async getResMv(e) {
      this.$api.simiMv({ mvid: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveResMv({ resMv: ret });
      });
    },

    // 获取MV基础信息
    async getItem(e) {
      this.loading = true;
      this.$api.mvDetail({ mvid: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveMvBes({ mvBes: ret });
        this.$router.push(`/mvPlay?id=${e}`);
      });
    },
  },
};
</script>

<style scoped>
.content {
  display: flex;
  flex-wrap: wrap;
}

.contentBox {
  margin-right: 5px;
}

.more {
  cursor: pointer;
  margin-top: 10px;
  color: #606266;
}

.el-card {
  margin-bottom: 10px;
  cursor: pointer;
  width: 300px;
  position: relative;
}

.el-card .el-image {
  width: 300px;
  height: 200px;
}

.desc {
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.loading {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
}

.el-icon-loading {
  position: fixed;
  font-size: 50px;
  left: 50%;
  top: 50%;
  color: #ec4141;
}
</style>
